package com.chenxingnet.filter;



import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.chenxingnet.dto.Auth;
import com.chenxingnet.dto.Publicity;
import com.chenxingnet.dto.Remind;
import com.chenxingnet.dto.Role;
import com.chenxingnet.dto.UserDepartment;
import com.chenxingnet.dto.Usercontrol;
import com.chenxingnet.dto.vo.meetVo;
import com.chenxingnet.service.IAuthService;
import com.chenxingnet.service.IPublicityService;
import com.chenxingnet.service.IRemindService;
import com.chenxingnet.service.ManagerService;
import com.chenxingnet.service.UserDeptService;
import com.chenxingnet.tools.MD5Tools;

public class AuthBackFilter  implements Filter{
	
	
	@Autowired
	IAuthService   authService;  
	@Autowired
	IRemindService   remindService; 
	@Autowired
	IPublicityService   publicityService; 
	@Autowired
	ManagerService managerService;
	@Autowired
	UserDeptService userDeptService;

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
	
		 HttpServletRequest request = (HttpServletRequest)req;
		 HttpServletResponse response = (HttpServletResponse)res;
		
		 ServletContext sc = request.getSession().getServletContext();
		 XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
         if(cxt != null && cxt.getBean("remindService") != null && remindService == null){
        	 remindService = (IRemindService) cxt.getBean("remindService");	  
         }
         if(cxt != null && cxt.getBean("publicityService") != null && publicityService == null){
        	 publicityService = (IPublicityService) cxt.getBean("publicityService");	  
         }
         if(cxt != null && cxt.getBean("managerService") != null && managerService == null){
        	 managerService = (ManagerService) cxt.getBean("managerService");	  
         }
         if(cxt != null && cxt.getBean("userDeptService") != null && userDeptService == null){
        	 userDeptService = (UserDeptService) cxt.getBean("userDeptService");	  
         }
         //判断是否生成超时提醒
         remindService.getDaySub();
         
         List<Remind>  resr=  remindService.getAllReminds();//获取提醒列表
      	 request.setAttribute("resr",resr);
      	
         List<Publicity>  meets = null;
         try {
        	try {
        		
        		Integer deptid = Integer.valueOf(request.getParameter("deptid"));
        		String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
				//根据登陆者信息显示对应部门的会议信息
         		if( (deptid != null && rolename.equals("部门经理")) || (deptid != null && rolename.equals("业务员")) ){
         			meets=  publicityService.getAllMeetByDept(deptid);//获取会议列表
         		}else {
         			meets=  publicityService.getAllMeetInfo();//获取会议全部信息
				}
         		
         		
			} catch (Exception e) {
				
         		String  uAccount=request.getParameter("username");
         		String  uPassword=request.getParameter("pwd");
         		Role role = null;
         		UserDepartment userDept = null;
         		//调用service层userLogin方法，根据账号和密码判断登录信息是否正确
         		Usercontrol user = managerService.userLogin(uAccount,MD5Tools.MD5(uPassword) );
         		if(user!=null){
         			role = managerService.selectByRid(user.getRoleid());
         			userDept = userDeptService.selectByUid(user.getUid());
         		}
         		
         		//根据登陆者信息显示对应部门的会议信息
         		if( (userDept.getDeptid() != null && role.getRolename().equals("部门经理")) || (userDept.getDeptid() != null && role.getRolename().equals("业务员")) ){
         			//List<Publicity>  
         			meets=  publicityService.getAllMeetByDept(userDept.getDeptid());//获取会议列表
         		}else {
         			meets=  publicityService.getAllMeetInfo();//获取会议全部信息
				}
				
			}
 		} catch (Exception e) {
 			//List<Publicity>  
 			meets=  publicityService.getAllMeetInfo();//获取会议全部信息
 		}
         //会议列表
         request.setAttribute("meets",meets);
         
        
     	
		if(request.getRequestURI().toString().contains("/mobile")||request.getRequestURL().toString().contains("huayang/resources")||request.getRequestURL().toString().contains("HuaYangImg")||request.getRequestURL().toString().contains("/js/")||request.getRequestURI().toString().equals("/huayang/")||request.getRequestURI().toString().equals("/huayang/upload/")){
				chain.doFilter(request, response);
		}else{
				Usercontrol user = (Usercontrol)request.getSession().getAttribute("user");
		        if( user != null){
		        	
		        	          if(cxt != null && cxt.getBean("authService") != null && authService == null){
		        	        	  authService = (IAuthService) cxt.getBean("authService");	  
		        	          }
		        	List<Auth>    auths=  this.authService.getOneUserAuthByRole(user.getRoleid());
		        	boolean   access =false;
		        	for(int   i=0;i<auths.size();i++){
		        		if(request.getRequestURL().toString().contains(auths.get(i).getAuthurl())){
		        			access=true;
		        		}
		        	}
		        	if(access){
		        		request.setAttribute("result","对不起，您无此操作权限。");
		        		chain.doFilter(request, response);
		        		
		        	}else{
		        		chain.doFilter(request, response);
		        	}        	
		        }else{        	
		        	request.setAttribute("result","访问系统，请先登录");
		     	    request.getRequestDispatcher("/back/userLogin").forward(request, response);	
		        }
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
	}

}
